<script setup lang="ts">
import { homeBanner, homeCategory, homeHot } from '@/apis/home';
import CustomNavBar from './components/CustomNavbar.vue'
import type { ResultBanner, ResultCategory, ResultHot } from '@/types/home'
import { onLoad } from '@dcloudio/uni-app';
import { ref } from 'vue'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
//保存轮播图响应式数据
const bannerList = ref<ResultBanner[]>([])

//获取广告区域轮播图
const getHomeBanner = async () => {
  const res = await homeBanner()
  bannerList.value = res.result
}

//保存首页分类响应式数据
const categoryList = ref<ResultCategory[]>([])
//获取前台分类
const getHomeCategory = async () => {
  const res = await homeCategory()
  categoryList.value = res.result
}

//保存热门推荐响应式数据
const hotList = ref<ResultHot[]>([])
//获取热门推荐
const getHomeHot = async () => {
  const res = await homeHot()
  hotList.value = res.result
}

onLoad(async () => {
  isLoading.value = true
  await Promise.all([getHomeBanner(), getHomeCategory(), getHomeHot()])
  isLoading.value = false
})

//引入猜你喜欢数据
import { useGuessLike } from '@/composables'
const { guessRef: xtxGuessRef, onScrolltolower } = useGuessLike()

//下拉刷新
const isTriggered = ref(false)
const onRefresherrefresh = async () => {
  //数据加载之前开启下拉刷新
  isTriggered.value = true
  xtxGuessRef.value?.resetRes()
  //获取页面数据
  await Promise.all([getHomeBanner(), getHomeCategory(), getHomeHot(), xtxGuessRef.value?.getGoodsLike()])
  // 关闭下拉刷新
  isTriggered.value = false
}

//引入骨架屏组件
import Skeleton from './components/Skeleton.vue'
const isLoading = ref(false)
</script>

<template>
  <!-- 自定义导航栏 -->
  <CustomNavBar></CustomNavBar>
  <scroll-view scroll-y class="scrollView" @scrolltolower="onScrolltolower" refresher-enabled
    @refresherrefresh=onRefresherrefresh :refresher-triggered="isTriggered">
    <!-- 骨架屏 -->
    <Skeleton v-if="isLoading"></Skeleton>
    <template v-else>
      <!-- 轮播图 -->
      <XtxSwiper :list="bannerList"></XtxSwiper>
      <!-- 首页分类 -->
      <CategoryPanel :categoryList="categoryList"></CategoryPanel>
      <!-- 热门推荐 -->
      <HotPanel :list="hotList"></HotPanel>
      <!-- 猜你喜欢 -->
      <XtxGuess ref="xtxGuessRef"></XtxGuess>
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #F7F7F7;
  height: 100%;
  display: flex;
  flex-direction: column;

  .scrollView {
    flex: 1;
    overflow: hidden;
  }
}
</style>
